﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Task.Entity;
using PowerCreator.LMS.Web.Task;
using System.Collections.Generic;

public partial class sysadmin_TaskManager_ManagerTarget : PowerCreator.LMS.Web.Base.BasePage
{
    EvaluateManag objEvaluateManag = new EvaluateManag();
    TargetManag objTargetManag = new TargetManag();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.Params["EvateID"] != null && Request.Params["EvateID"].Trim() != "")
            {
                int FKTargetEvateID = int.Parse(Request.Params["EvateID"].ToString());
                ViewState["FKTargetEvateID"] = FKTargetEvateID;

            }
            InitPage();

            BinderRpt();
        }
    }

    protected override void InitPage()
    {
        lbl_InfoTitle.Text = "评价指标管理"; //GetLanageValueByKey("lbl_Department_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);

        base.InitPage();
    }

    //绑定Rpt
    private void BinderRpt()
    {

        string search = "";
        if(ViewState["FKTargetEvateID"]!=null)
        {
            search = " FKTargetEvateID=" + ViewState["FKTargetEvateID"].ToString();
        }

        DataTable dt = objTargetManag.Query(search);
        List<Target> list = new List<Target>();
        list = objTargetManag.GetTargetTree(dt);
        
        
        //if (list.Count == 0)
        //{
        //    Target li = new ListItem();
        //    li.Text = "顶级指标"; //GetLanageValueByKey("department_defaultdeparmentname", PowerCreator.LMS.Language.LanagetItemType.Page);
        //    li.Value = "0";
        //    list.Add(li);
        //}

        rptTarget.DataSource = list;
        rptTarget.DataBind();
        for (int i = 0; i < rptTarget.Items.Count; i++)
        {
            Button btnadd = (Button)rptTarget.Items[i].FindControl("btn_Add");
            btnadd.Text = GetLanageValueByKey("btn_AddNew", PowerCreator.LMS.Language.LanagetItemType.Page);
            Button btn_Editor = (Button)rptTarget.Items[i].FindControl("btn_Editor");
            btn_Editor.Text = GetLanageValueByKey("ManagerRole_gv_lnk_o1", PowerCreator.LMS.Language.LanagetItemType.Page);
            Button btn_del = (Button)rptTarget.Items[i].FindControl("btn_del");
            btn_del.Text = GetLanageValueByKey("ManagerRole_gv_lnk_o2", PowerCreator.LMS.Language.LanagetItemType.Page);
        }
    }

    protected void rptDepartment_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName.ToLower() == "btnadd")
        {
            string url = "AddTarget.aspx?" + e.CommandArgument.ToString();
            Script.Alert(e.CommandArgument.ToString());
            Script.RedirectURL(url);
        }
        else if (e.CommandName.ToLower() == "btnedit")
        {
            string url = "EditTarget.aspx?" + e.CommandArgument.ToString();
            Script.RedirectURL(url);
        }
        else if (e.CommandName.ToLower() == "btndel")
        {
            int targetID = int.Parse(e.CommandArgument.ToString());
            string search = " TargetFatherID=" + targetID.ToString();
            int count = objTargetManag.Count(search);
            if (count > 1)
            {
                //Label lblmsg = (Label)e.Item.FindControl("lbl_msg_description");
                lbl_msg_description.Text = "该项底下有子节点,不允许删除"; //GetLanageValueByKey("department_msg_g1", PowerCreator.LMS.Language.LanagetItemType.Page);
                
                //Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
                return;
            }

            if (objTargetManag.Remove(targetID))
            {
                int evateID=int.Parse(ViewState["FKTargetEvateID"].ToString());
                objTargetManag.UpdateTargetScore(evateID);
                BinderRpt();
            }
        }
    }
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        Script.RedirectURL("AddTarget.aspx?EvateID=" + ViewState["FKTargetEvateID"].ToString());
    }
    protected void btn_Back_Click(object sender, EventArgs e)
    {
        Script.RedirectURL("EvaluateManager.aspx");
    }
    protected void rptTarget_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {

        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            Button lbtn = (Button)e.Item.FindControl("btn_del");
            lbtn.Attributes.Add("onClick", "javascript:return confirm('确定删除此记录？');");

            Label lblTargetFatherID = (Label)e.Item.FindControl("lblTargetFatherID");
            Label lblTargetScore = (Label)e.Item.FindControl("lblTargetScore");
            Label lblTargetScale = (Label)e.Item.FindControl("lblTargetScale");
            Label lblTargetType = (Label)e.Item.FindControl("lblTargetType");
            Label lblTargetTypeValue = (Label)e.Item.FindControl("lblTargetTypeValue");
            
            int targetTypeID = int.Parse(lblTargetType.Text);
            string targetTypeName = objTargetManag.ConvertTargetType(targetTypeID);
            lblTargetType.Text = targetTypeName;

            if (targetTypeID == 0)
            {
                lblTargetScore.Text = "- -";
                lblTargetScale.Text = "- -";
            }

            //string search = " TargetFatherID=" + lblTargetScore.Text.ToString();
            //int count = objTargetManag.Count(search);
            //if (count > 1)
            //{
            //    lblTargetScore.Text = "- -";
            //    lblTargetScale.Text = "- -";
            //}
    

        }
    }
}
